A good answer might be:

Two things: a variable of type Color will be added to the class, and an access method to allow the user to set it.


Setting Colors

Here is the revised class with these two things added. A few other changes were made in the constructors since there is another variable to initialize.

class Circle
{
  // variables
  private int x, y, radius;
  private Color color;

  // constructors
  public Circle()
  { x = 0; y = 0; radius = 0; color = Color.black;}

  public Circle( int x, int y, int radius )
  { this.x = x;  this.y = y;  this.radius = radius; color = Color.black;}

  // methods
  void draw( Graphics gr )
  {
    int ulX = x-radius ; // X of upper left corner of rectangle
    int ulY = y-radius ; // Y of upper left corner of rectangle
    gr.setColor( color );
    gr.drawOval( ulX, ulY, 2*radius, 2*radius );
  }

  // change the center of the circle to a new X and Y
  void setPosition( int newX, int newY )
  {
    x = newX ;
    y = newY ;
  }

  // chage the radius of the circle
  void setRadius( int newR )
  {
    radius = newR;
  }
  
  // chage the color of the circle
  void setColor( Color newC )
  {
    color = newC;
  }
  
}

The setColor(Color newC) method of the Graphics class uses a parameter which is an object—an object of the Color class. Color objects are like String objects: they are immutable. That is, once a Color object has been created, it can not be changed. So our setColor() method is correct. The Color object it gets will never change, so the variable color can refer to it.

QUESTION 13:

If Color objects were not immutable, what would we have to worry about?